var t = require("../../../../@babel/runtime/helpers/interopRequireDefault"), e = t(require("../../../../@babel/runtime/regenerator")), a = require("../../../../@babel/runtime/helpers/asyncToGenerator"), n = t(require("../../../../utils/weapp.qrcode.esm.js")), o = getApp();

Component({
    properties: {
        isshow: {
            value: !1,
            type: Boolean
        },
        showcarrfootBol: {
            type: null
        },
        iPnoneBottomBol: {
            type: null
        }
    },
    data: {
        errorFlag: !1,
        errorMsg: "",
        poster: "",
        poster_id: 0,
        loading: 0,
        getFlag: !0,
        step: 1.5
    },
    ready: function() {},
    observers: {
        isshow: function(t) {
            t && "" == this.data.poster && this.data.getFlag && (this.getPosterPic(), this.data.getFlag = !1);
        }
    },
    methods: {
        loadImgError: function(t) {
            this.setData({
                errorFlag: !0,
                errorMsg: t.detail.errMsg
            });
        },
        clicktapshow: function() {
            this.setData({
                isshow: !1
            });
        },
        previewImage: function() {
            wx.showToast({
                title: "长按分享好友",
                icon: "none",
                duration: 1800
            }), wx.previewImage({
                urls: [ this.data.poster ]
            });
        },
        getPosterPic: function() {
            var t = this;
            return a(e.default.mark(function a() {
                var n, s;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, t.getMyCanvasMessage(640, 1008);

                      case 2:
                        n = e.sent, s = o.getNetAddresss("member.qrcode.get-poster"), t.setData({
                            canvasWidth: n.width,
                            canvasHeight: n.height
                        }), o._getNetWork({
                            showToastIn: !1,
                            url: s,
                            success: function(e) {
                                var a = e.data;
                                1 == a.result && (0 == a.data.center_show ? t.setData({
                                    loading: 100,
                                    poster: a.data.image_url
                                }) : 1 == a.data.center_show ? (t.data.poster_id = e.data.data.id, t.startDrawCanvas(n, e.data.data)) : 2 == a.data.center_show && t.getPosterTp2());
                            }
                        });

                      case 6:
                      case "end":
                        return e.stop();
                    }
                }, a);
            }))();
        },
        getPosterTp2: function() {
            var t = this, e = this, a = o.getNetAddresss("member.poster");
            o._getNetWork({
                url: a,
                success: function(a) {
                    var n = a.data;
                    1 == n.result ? "" == n.data.image_url || null == n.data.image_url ? (wx.showToast({
                        title: "请稍等" + n.data.image_url,
                        icon: "none",
                        duration: 2e3
                    }), setTimeout(function() {
                        t.getPosterTp2();
                    }, 500)) : "" != n.data.image_url && setTimeout(function() {
                        e.setData({
                            poster: n.data.image_url
                        });
                    }, 1e3) : wx.showToast({
                        title: n.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    this.data.timer && clearInterval(this.data.timer), wx.hideLoading({}), console.log(t);
                }
            });
        },
        startDrawCanvas: function(t, n) {
            var o = this;
            return a(e.default.mark(function s() {
                var r, i, c, l, u, d, h, g, f, w;
                return e.default.wrap(function(s) {
                    for (;;) switch (s.prev = s.next) {
                      case 0:
                        return (r = wx.createCanvasContext("myCanvas", o)).setFillStyle("#fff"), r.fillRect(0, 0, t.width, t.height), 
                        s.next = 5, o.readDownPicInfo(n.background);

                      case 5:
                        (i = s.sent).height > i.width ? (c = t.height / t.contentScale / i.height, i.height = i.height * c, 
                        i.width = i.width * c) : (l = t.width / t.contentScale / i.width, i.width = i.width * l, 
                        i.height = i.height * l), o.drawCanvasImage(r, {
                            left: 0,
                            top: 0,
                            width: i.width / o.data.step,
                            height: i.height / o.data.step,
                            img: i.img,
                            contentScale: t.contentScale
                        }), o.setData({
                            loading: 10
                        }), u = n.style_data, d = 0;

                      case 11:
                        if (!(d < u.length)) {
                            s.next = 39;
                            break;
                        }
                        if ((h = u[d]).contentScale = t.contentScale, console.log(h), "nickname" != h.type) {
                            s.next = 19;
                            break;
                        }
                        o.drawCanvasText(r, h), s.next = 33;
                        break;

                      case 19:
                        if ("img" != h.type && "head" != h.type) {
                            s.next = 27;
                            break;
                        }
                        return s.next = 22, o.readDownPicInfo(h.src);

                      case 22:
                        g = s.sent, h.img = g.img, o.drawCanvasImage(r, h), s.next = 33;
                        break;

                      case 27:
                        if ("qr_shop" != h.type && "qr_app_share" != h.type) {
                            s.next = 33;
                            break;
                        }
                        return s.next = 30, o.getQRCodeUrl(h);

                      case 30:
                        f = s.sent, h.img = f, o.drawCanvasImage(r, h);

                      case 33:
                        console.log("loading", w), w = parseInt(80 / u.length * (d + 1)), o.setData({
                            loading: w + 10
                        });

                      case 36:
                        d++, s.next = 11;
                        break;

                      case 39:
                        console.log("执行完成"), r.draw(!1, setTimeout(a(e.default.mark(function a() {
                            var n;
                            return e.default.wrap(function(e) {
                                for (;;) switch (e.prev = e.next) {
                                  case 0:
                                    return e.next = 2, o.toimage("myCanvas", t);

                                  case 2:
                                    n = e.sent, o.setData({
                                        loading: 100,
                                        poster: n
                                    }), o.upQrcodeImg(n);

                                  case 5:
                                  case "end":
                                    return e.stop();
                                }
                            }, a);
                        })), 1e3));

                      case 41:
                      case "end":
                        return s.stop();
                    }
                }, s);
            }))();
        },
        drawCanvasImage: function(t, e) {
            var a = this.data.step;
            t.drawImage(e.img, e.left * e.contentScale * a, e.top * e.contentScale * a, e.width * e.contentScale * a, e.height * e.contentScale * a);
        },
        drawCanvasText: function(t, e) {
            var a = this.data.step;
            console.log(a);
            var n = e.size * e.contentScale, o = e.top * e.contentScale + n;
            t.setFontSize(n * a), t.setFillStyle(e.color), t.fillText(e.src, e.left * e.contentScale * a, o * a), 
            console.log("完成");
        },
        toimage: function(t, e) {
            var a = this;
            return new Promise(function(n, o) {
                console.log(t, e), wx.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: e.width,
                    height: e.height,
                    destWidth: 4 * e.width,
                    destHeight: 4 * e.height,
                    canvasId: t,
                    success: function(t) {
                        var e = t.tempFilePath;
                        n(e);
                    },
                    fail: function(t) {
                        wx.showToast({
                            title: "图片导出失败" + t,
                            icon: "success",
                            duration: 2e3
                        });
                    }
                }, a);
            });
        },
        drawRoundRectPath: function(t, e, a, n) {
            t.beginPath(0), t.arc(e - n, a - n, n, 0, Math.PI / 2), t.lineTo(n, a), t.arc(n, a - n, n, Math.PI / 2, Math.PI), 
            t.lineTo(0, n), t.arc(n, n, n, Math.PI, 3 * Math.PI / 2), t.lineTo(e - n, 0), t.arc(e - n, n, n, 3 * Math.PI / 2, 2 * Math.PI), 
            t.lineTo(e, a - n), t.closePath(), t.clip();
        },
        readDownPicInfo: function(t) {
            var e = this;
            return new Promise(function(a, n) {
                wx.downloadFile({
                    url: t,
                    success: function(t) {
                        wx.getImageInfo({
                            src: t.tempFilePath,
                            success: function(e) {
                                a({
                                    img: t.tempFilePath,
                                    width: e.width,
                                    height: e.height
                                });
                            },
                            fail: function(t) {
                                e.data.getFlag = !0, console.log("图片读取出错", t.errMsg), wx.showToast({
                                    title: "图片读取出错" + t.errMsg,
                                    icon: "success",
                                    duration: 2e3
                                });
                            }
                        });
                    },
                    fail: function(t) {
                        e.data.getFlag = !0, console.log("图片下载出错", t), wx.showToast({
                            title: "图片下载出错" + t,
                            icon: "none",
                            duration: 2e3
                        });
                    }
                });
            });
        },
        getMyCanvasMessage: function(t, e) {
            var a = this;
            return new Promise(function(n, o) {
                wx.getSystemInfo({
                    success: function(o) {
                        var s = e / t, r = o.screenWidth - .2 * o.screenWidth, i = r * s, c = r / t;
                        r *= a.data.step, i *= a.data.step, n({
                            width: r,
                            height: i,
                            contentScale: c
                        });
                    },
                    fail: function(t) {
                        a.data.getFlag = !0;
                    }
                });
            });
        },
        getQRCodeUrl: function(t) {
            var o = this;
            return new Promise(function(s, r) {
                var i = o.data.step;
                o.setData({
                    tempQRCodeCanvasWidth: t.width * t.contentScale * i,
                    tempQRCodeCanvasHeight: t.height * t.contentScale * i
                }, a(e.default.mark(function r() {
                    return e.default.wrap(function(r) {
                        for (;;) switch (r.prev = r.next) {
                          case 0:
                            (0, n.default)({
                                width: (t.width * t.contentScale - 10) * i,
                                height: (t.height * t.contentScale - 10) * i,
                                canvasId: "tempQRCode",
                                background: "#fff",
                                _this: o,
                                x: 5 * i,
                                y: 5 * i,
                                text: t.src,
                                callback: function() {
                                    setTimeout(a(e.default.mark(function a() {
                                        var n;
                                        return e.default.wrap(function(e) {
                                            for (;;) switch (e.prev = e.next) {
                                              case 0:
                                                return e.next = 2, o.toimage("tempQRCode", {
                                                    width: t.width * t.contentScale * i,
                                                    height: t.height * t.contentScale * i
                                                });

                                              case 2:
                                                n = e.sent, s(n);

                                              case 4:
                                              case "end":
                                                return e.stop();
                                            }
                                        }, a);
                                    })), 500);
                                }
                            });

                          case 1:
                          case "end":
                            return r.stop();
                        }
                    }, r);
                })));
            });
        },
        pxToNum: function(t) {
            var e = t.substring(0, t.length - 2);
            return parseInt(e);
        },
        upQrcodeImg: function(t) {
            var e = this, a = o.getNetAddresss("upload.uploadPic");
            console.log(t), wx.uploadFile({
                url: a,
                filePath: t,
                name: "file",
                header: {
                    "Content-Type": "multipart/form-data"
                },
                formData: {
                    _wx_token: wx.getStorageSync("wx_token"),
                    ingress: 0
                },
                success: function(t) {
                    var a = JSON.parse(t.data);
                    console.log("上传成功", a), 1 == a.result && (console.log(a.data.img_url), e.addPosterRecord(a.data.img_url));
                },
                fail: function(t) {
                    console.log("上传失败一一一", t);
                }
            });
        },
        addPosterRecord: function(t) {
            var e = o.getNetAddresss("member.qrcode.poster-record");
            console.log("开始写记录"), o._getNetWork({
                showToastIn: !1,
                url: e,
                data: {
                    image: t,
                    poster_id: this.data.poster_id
                },
                success: function(t) {}
            });
        },
        checkWritePotosAlbum: function() {
            var t = this;
            return a(e.default.mark(function a() {
                var n;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return n = t, e.next = 3, t.getAuthSetting("scope.writePhotosAlbum");

                      case 3:
                        if (e.sent) {
                            e.next = 7;
                            break;
                        }
                        return wx.showModal({
                            title: "请开启授权",
                            content: "是否跳转设置页开启授权",
                            success: function(t) {
                                t.confirm ? wx.openSetting({
                                    success: function(t) {
                                        t.authSetting["scope.writePhotosAlbum"] ? n.saveImg() : console.log("还是没授权");
                                    }
                                }) : t.cancel && console.log("用户点击取消");
                            }
                        }), e.abrupt("return");

                      case 7:
                        t.saveImg();

                      case 8:
                      case "end":
                        return e.stop();
                    }
                }, a);
            }))();
        },
        saveImg: function() {
            console.log("执行");
            var t = this.data.poster;
            wx.downloadFile({
                url: t,
                success: function(t) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t.tempFilePath,
                        success: function(t) {
                            wx.showToast({
                                title: "保存成功",
                                icon: "none",
                                duration: 2e3
                            });
                        },
                        fail: function(t) {
                            console.log(t);
                        }
                    });
                }
            });
        },
        getAuthSetting: function(t) {
            return new Promise(function(e, a) {
                wx.getSetting({
                    success: function(a) {
                        a.authSetting[t] ? e(!0) : wx.authorize({
                            scope: t,
                            success: function() {
                                e(!0);
                            },
                            fail: function() {
                                e(!1);
                            }
                        });
                    },
                    fail: function() {
                        e(!1);
                    }
                });
            });
        }
    }
});